package com.shopping_back.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.shopping_back.common.Result;
import com.shopping_back.pojo.dto.NoticeCreateDTO;
import com.shopping_back.pojo.dto.NoticeUpdateDTO;
import com.shopping_back.pojo.entity.Notice;
import com.shopping_back.service.INoticeService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统通知 / 公告 —— 后台管理接口
 */
@RestController
@RequestMapping("/api/notice")
@RequiredArgsConstructor
public class NoticeAdminController {

    private final INoticeService noticeService;
    @GetMapping("/list")
    public Result list(@RequestParam(required = false) Integer type) {
        return Result.success(noticeService.listAll(type), null);
    }

    /** 分页列表（按标题 / 类型搜索） */
    @GetMapping("/admin/list")
    public Result list(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @RequestParam(required = false) String title,
                       @RequestParam(required = false) Integer type) {
        return Result.success(noticeService.listPage(pageNum, pageSize, title, type));
    }

    /** 新增 */
    @PostMapping("/admin/add")
    public Result add(@RequestBody @Validated NoticeCreateDTO dto) {
        noticeService.create(dto);
        return Result.success();
    }

    /** 编辑 */
    @PutMapping("/admin/edit")
    public Result edit(@RequestBody @Validated NoticeUpdateDTO dto) {
        noticeService.updateBasic(dto);
        return Result.success();
    }

    /** 删除 */
    @DeleteMapping("/admin/{id}")
    public Result delete(@PathVariable Long id) {
        noticeService.removeById(id);
        return Result.success();
    }
}

